package com.eralink.unionuhfreader.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.CateProduct;

import com.eralink.unionuhfreader.util.ToastUtil;


import com.eralink.unionuhfreader.util.ToastUtil;

import java.math.BigDecimal;
import java.util.List;

public class ManualCateRecycleViewAdapter extends RecyclerView.Adapter<ManualCateRecycleViewAdapter.ViewHold> {

    private List<CateProduct> mList;//数据源
    private Context mContext;
    private CalCount calCount;

    public void setCalCount(CalCount calCount) {
        this.calCount = calCount;
    }

    public ManualCateRecycleViewAdapter(Context context, List<CateProduct> vos) {
        this.mContext = context;
        this.mList = vos;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_manucateproduct, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHold holder = new ViewHold(view);
        //返回这个MyHolder实体
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, @SuppressLint("RecyclerView") int position) {
        /*if (mList.get(position).getImgUrl() != null && !mList.get(position).getImgUrl().equals("")) {
            try{
                JSONArray imgs= JSON.parseArray(mList.get(position).getImgUrl());
                if (imgs.size()>0){
                    Glide.with(mContext).load(imgs.get(0).toString()).into(holder.img_product).onLoadFailed(null, mContext.getResources().getDrawable(R.mipmap.logo));
                }else{
                    Glide.with(mContext).load(mContext.getResources().getDrawable(R.mipmap.logo));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            Glide.with(mContext).load(mContext.getResources().getDrawable(R.mipmap.logo));
        }*/
        holder.productname.setText(mList.get(position).getProductName());
        holder.price.setText("价格:"+mList.get(position).getSalePrice() + "");
        holder.tv_single_counts.setText(mList.get(position).getCount()+"");
        holder.tx_single_price.setText("总计:"+new BigDecimal(mList.get(position).getSalePrice()).multiply(new BigDecimal(mList.get(position).getCount())));
        holder.tx_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){
                    calCount.addOrDivClick(position,1);
                }
            }
        });
        holder.tx_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){

                    if (mList.get(position).getCount()==1){
                        ToastUtil.showToast("商品数量不能低于1");
                        return;
                    }
                    calCount.addOrDivClick(position,-1);
                }
            }
        });
        holder.tx_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (calCount!=null){
                    calCount.addOrDivClick(position,0);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHold extends RecyclerView.ViewHolder {
        private TextView productname,tx_jian,tx_add;
        private TextView inv, price,tv_single_counts,tx_del,tx_single_price;
        private ImageView img_product;

        public ViewHold(View convertView) {
            super(convertView);
            productname = (TextView) convertView.findViewById(R.id.productname);
            inv = (TextView) convertView.findViewById(R.id.inv);
            price = (TextView) convertView.findViewById(R.id.price);
            img_product = (ImageView) convertView.findViewById(R.id.img_product);
            tv_single_counts=convertView.findViewById(R.id.tv_single_counts);
            tx_jian=convertView.findViewById(R.id.tx_jian);
            tx_add=convertView.findViewById(R.id.tx_add);
            tx_del=convertView.findViewById(R.id.tx_del);
            tx_single_price=convertView.findViewById(R.id.tx_single_price);
        }
    }
    public interface  CalCount{
        void addOrDivClick(int postion,int count);
    }
}
